package com.service.impl;

import com.bean.LoginLog;
import com.bean.User;
import com.dao.LoginLogDao;
import com.dao.UserDao;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cyf
 * @description
 * @create 2018-09-10 15:53
 **/
@Service
public class UserServiceImpl implements UserService {

    private static final int INTEGRAL = 5;

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;


    @Override
    public boolean getMatchCount(String userName, String passWord) {
        return userDao.getMatchCount(userName, passWord) > 0;
    }

//    @Transactional
    @Override
    public void loginSuccess(User user) {
        // 用户积分 + 5
        user.setIntegral(INTEGRAL + user.getIntegral());
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(user.getUserId());
        loginLog.setLoginIp(user.getLoginIp());
        loginLog.setLoginDate(user.getLoginDate());

        // 更新用户积分
        userDao.updateLoginInfo(user);

        // 保存用户登陆日志
        loginLogDao.insertLoginLog(loginLog);
    }

    @Override
    public User getUserByUserName(String userName) {
        return userDao.getUserByUserName(userName);
    }

    @Override
    public void insertLoginLog(LoginLog loginLog) {
        loginLogDao.insertLoginLog(loginLog);
    }
}
